package com.masterclient.app.businessmodule.minemodule.message.contract;

import com.eoner.baselib.enums.FCPageViewState;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.baselibrary.bean.personal.OtherMsgBean;
import com.eoner.baselib.container.CommonBaseResponse;
import com.masterclient.app.businessmodule.minemodule.api.MineHttpService;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

public class OtherMsgPresenter extends OtherMsgContract.Presenter {

    boolean isLoad = false;
    @Override
    public void getMsgList(int page,boolean isRefresh) {
        if (page == 1 && !isRefresh) {
            mView.baseShowPageLoading(false);
        }
        mRxManager.add(toSubscribe(MineHttpService.getInstance().getOtherMsg(String.valueOf(page)), new Consumer<CommonBaseResponse<OtherMsgBean>>() {
            @Override
            public void accept(@NonNull CommonBaseResponse<OtherMsgBean> bean) throws Exception {
                mView.baseHiddenPageLoading();
                mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NORMAL);
                if(bean.getCode() == 0){
                    isLoad = true;
                    mView.showData(bean.getData());
                } else {
                    if (!isLoad) {
                        mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                    }

                    mView.showFailure(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                o.printStackTrace();
                if (!isLoad) {
                    mView.baseRefreshPageState(FCPageViewState.PAGE_STATE_NET_ERROR);
                }
                mView.baseHiddenPageLoading();
                mView.showFailure("网络异常，请重试");
            }
        }));


    }

}
